package
{
	
	import com.adobe.images.JPGEncoder;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Rectangle;
	import flash.html.HTMLLoader;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.utils.ByteArray;
	import nochump.util.zip.ZipEntry;
	import nochump.util.zip.ZipFile;
	import nochump.util.zip.ZipOutput;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class Main extends Sprite
	{
		
		private var myFileReference:FileReference;
		
		public function Main():void
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		
		}
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//readZip();
			saveZip();
		}
		
		//压zip
		private function saveZip():void
		{
			var fileName:String = "helloworld.txt";
			var fileName2:String = "image.jpg";
			var fileData:ByteArray = new ByteArray();
			fileData.writeUTFBytes("Hello World!中文");
			var zipOut:ZipOutput = new ZipOutput();
			// Add entry to zip
			var ze:ZipEntry = new ZipEntry(fileName);//这是一个文件
			var ze2:ZipEntry = new ZipEntry(fileName2);//这是一个文件
			//
			//var jpg = new bmd(100, 100);
			var jpg:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
			jpg.draw(stage);
			var je:JPGEncoder = new JPGEncoder();
			var pixels:ByteArray = je.encode(jpg);
			zipOut.putNextEntry(ze2);
			zipOut.write(pixels);
			//zipOut.closeEntry();
			//
			zipOut.putNextEntry(ze);
			zipOut.write(fileData);
			zipOut.closeEntry();
			// end the zip
			zipOut.finish();
			// access the zip data
			var zipData:ByteArray = zipOut.byteArray;
			myFileReference = new FileReference();
			myFileReference.addEventListener(Event.COMPLETE, readZip);
			myFileReference.save(zipData, "qq.zip");
		
		}
		
		//解zip
		private function readZip(e:Event = null):void
		{
			var urlStream:URLStream = new URLStream();
			urlStream.addEventListener(Event.COMPLETE, completeHandler);
			urlStream.load(new URLRequest("zip.zip"));
		}
		
		private function completeHandler(event:Event):void
		{
			var datastream:URLStream = URLStream(event.target);
			var kmzFile:ZipFile = new ZipFile(datastream);
			trace(kmzFile.entries.join("@").split(".jpg").length);
			//totalMaterials = kmzFile.entries.join("@").split(".jpg").length;
			trace(kmzFile);
			trace(kmzFile.entries);
			trace(kmzFile.entries.length);//总文件数
			trace(kmzFile.entries.join("@").split(".jpg").length);//顶目录的长度
			for (var i:int = 0; i < kmzFile.entries.length; i++)
			{
				var entry:ZipEntry = kmzFile.entries[i];
				var data:ByteArray = kmzFile.getInput(entry);
				if ((entry.name.indexOf(".jpg") > -1 || entry.name.indexOf(".png") > -1))
				{
					var _loader:Loader = new Loader();
					_loader.name = entry.name.split("/").reverse()[0].split(".")[0];
					_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadBitmapCompleteHandler);
					_loader.loadBytes(data);
				}
				else if (entry.name.indexOf(".txt") > -1)
				{
					trace(data.toString());
				}
			}
		}
		
		private function loadBitmapCompleteHandler(e:Event):void
		{
			var loader:Loader = Loader(e.target.loader);
			var bitmap:Bitmap = Bitmap(loader.content);
			addChild(bitmap);
			trace(this.numChildren);
		}
	}

}